/**
 * 2024/01/01 - 2024/12/31
 **/
package com.snail.camunda.engin.identity.manager;

import com.snail.camunda.engin.identity.dto.GroupInsertDTO;
import com.snail.camunda.engin.identity.dto.GroupModifyDTO;
import com.snail.camunda.engin.identity.dto.GroupQueryDTO;
import com.snail.camunda.engin.identity.holder.GroupHolder;
import com.snail.camunda.engin.identity.holder.TenantHolder;
import com.snail.camunda.engin.identity.mapping.GroupMapping;
import com.snail.camunda.engin.identity.vo.GroupVO;
import com.snail.starter.commons.page.PageQO;
import com.snail.starter.commons.page.PageVO;
import org.camunda.bpm.engine.identity.Group;
import org.camunda.bpm.engine.identity.Tenant;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @author: (zg)
 * @Date: 2024/12/16 16:53
 * @Version: 1.0.0
 */
@Component
public class GroupManager {

    @Resource
    GroupHolder groupHolder;

    @Resource
    GroupMapping groupMapping;

    @Resource
    TenantHolder tenantHolder;

    public String add(String tenantId, GroupInsertDTO dto) {
        Tenant tenant = tenantHolder.queryById(tenantId);
        return groupHolder.add(tenant, dto);
    }

    public boolean modify(String id, GroupModifyDTO dto) {
        return groupHolder.modify(id, dto);
    }

    public boolean delete(String id) {
        return groupHolder.deleteById(id);
    }

    public GroupVO queryById(String id) {
        return groupMapping.entity2Vo(groupHolder.queryById(id));
    }

    public List<GroupVO> list(GroupQueryDTO dto) {
        List<Group> list = groupHolder.list(dto);
        if(null == list || list.isEmpty()){
            return new ArrayList<>();
        }
        return list.stream().map(groupMapping::entity2Vo).collect(Collectors.toList());
    }

    public PageVO<GroupVO> page(PageQO<GroupQueryDTO> page) {
        PageVO<Group> pageVO = groupHolder.page(page);
        return PageVO.of(pageVO.getPageNum(), pageVO.getPageSize(), pageVO.getTotal(), pageVO.getData().stream().map(groupMapping::entity2Vo).collect(Collectors.toList()));
    }
}